#ifndef TCPMGR_H
#define TCPMGR_H
#include "Singleton.h"
#include <QOBJECT>
#include<QTcpSocket>
#include"global.h"
#include "userdata.h"
#include"usermgr.h"
#include<memory>

class TcpMgr:public QObject,public Singleton<TcpMgr>,public std::enable_shared_from_this<TcpMgr>
{
    Q_OBJECT
    friend class Singleton<TcpMgr>;
public:
    ~TcpMgr();
    TcpMgr(const TcpMgr&) = delete;
    TcpMgr operator=(const TcpMgr&) = delete;
private:
    TcpMgr();
    void initHandler();
    QMap<ReqId,std::function<void(const QJsonObject&)>> _handlers;
    void handleMsg(ReqId id,QByteArray data); // 数据处理
    QTcpSocket _socket;
    QString _host;
    uint16_t _port;
    QByteArray _buffer; // 收到数据的buffer 用于缓存
    bool _bRecvPending; // 是否数据收全 true没收全 false 收全了
    quint16 _messageId;
    quint16 _messageLen;

public slots:
    void SlotTcpConnect(ServerInfo);              // 建立链接
    void SlotSendData(ReqId reqId, QString data); // 发送数据
signals:
    void SigConSuccess(bool bsuccess);
    void SigSwitchChatDLG();
    void SigLoginFaild(int);
    void SigSendData(ReqId reqId, QString data);
    void SigUserSearch(std::shared_ptr<SearchInfo> si);
    void SigFriendApply(std::shared_ptr<AddFriendApply> apply);
    void SigAuthRsp(std::shared_ptr<AuthRsp>);
    void SigAddAuthFriend(std::shared_ptr<AuthInfo>);
    void SigTextChatMsg(std::shared_ptr<TextChatMsg> msg);

};

#endif // TCPMGR_H
